'use strict';
const AV = require('leanengine');

/**
 * 购物车列表
 */
exports.list = function (req, res, next) {

    new AV.Query('Cart').ascending('order').find().then(function (results) {

        returnJson.code = 200;
        returnJson.data = results;

        res.json(returnJson);
    }).catch(function (err) {
        next(err);
    });

};

/**
 * 加入购物车
 * @param req
 * @param res
 * @param next
 */
exports.add = function (req, res, next) {

    const goodsId = parseInt(req.body.goodsId) || 0;
    const buyNumber = parseInt(req.body.buyNumber) || 1;

    if (!goodsId) {
        res.json({
            code: 400,
            msg:  '商品不存在',
            data: ''
        });
    }



    (async function () {
        //查看商品是否可购买
        let goods = await new AV.Query('Goods').equalTo('goodsId', goodsId).equalTo('isDelete', 0).first();
        if (!goods){
            return res.json({
                code: 400,
                message:  '商品不存在',
                data: ''
            });
        }

        //判断是否已下架
        if (goods.get('isSale') == 0){
            return res.json({
                code: 400,
                message:  '商品已下架',
                data: ''
            });
        }

        //判断库存 TODO
        // if (goods.get('number') <= 0){
        //     return res.json({
        //         code: 400,
        //         msg:  '商品已售完',
        //         data: ''
        //     });
        // }

        //判断是否存在购物车中
        let cart = await new AV.Query('Cart').equalTo('goodsId', goodsId).first();
        if (cart){
            //更新库存数量
            cart.set('number', buyNumber);
            let saveRes = await cart.save();
            return res.json({message: cart});
        }else{

            //添加到购物车
            var Cart = AV.Object.extend('Cart');
            var newCart = new Cart();
            newCart.set('number', buyNumber);
            newCart.set('marketPrice', parseFloat(goods.get('marketPrice')));
            newCart.set('shopPrice', parseFloat(goods.get('shopPrice')));
            newCart.set('goodsId', goodsId);
            newCart.set('goodsName', goods.get('name'));
            newCart.set('image', goods.get('image').get('url'));
            console.log(newCart);

            newCart.save().then(function (info) {
                return res.json({
                    code: 200,
                    message:  '成功添加到购物车',
                    data: ''
                });
            }).catch(function (err) {
                next(err);
            });
        }

    })();
};

/**
 * 删除购物车商品
 * @param req
 * @param res
 * @param next
 */
exports.drop = function (req, res, next) {
    const cartId = parseInt(req.query.cartId) || 0;

    if (!cartId) {
        res.json({
            code: 400,
            message:  '商品不存在',
            data: ''
        });
    }

    new AV.Query('Cart').
    equalTo('cartId', cartId).first().then(function (cart) {
        if(!cart){
            res.json({
                code: 400,
                message:  '商品不存在',
                data: ''
            });
        }
        cart.destroy().then(function (success) {
            // 删除成功
            res.json({
                code: 200,
                message:  '成功删除商品',
                data: ''
            });
        }, function (error) {
            // 删除失败
            res.json({
                code: 400,
                message:  '删除失败',
                data: ''
            });
        });
    }).catch(function (err) {
        next(err);
    });

};